// <copyright file="EasyWebApp.cs" company="<Customer Name>">This is a "work-for-hire". or Copyright (C) 2010 MicroCODE Incorporated Troy, MI</copyright><author><Class Author's Name></author>

/*
 *      Title:    <Class Description>
 *      Module:   ClassTemplate (<Namespace>:ClassTemplate.cs)
 *      Project:  <Project Name>
 *      Customer: <Customer Name>
 *      Facility: MicroCODE Incorporated
 *      Date:     <Class Creation Date>
 *      Author:   <Class Author's Name>
 * 
 * 
 * 
 *  
 *      MODIFICATIONS:
 *      --------------
 *
 *  Date:         By-Group:   Rev:     Description:
 *
 *  DD-Mmm-YYYY   XYZ-GROUP  {0001}    Describe reason for creating or modifying class... 
 * 
 * 
 * 
 */
using System;
using System.Reflection;
using System.Resources;
using System.Xml.XPath;
using Org.EasyWeb.SysUtil;
using Org.EasyWeb.Xml;

namespace Org.EasyWeb.Xml
{
    public class BaseTransform : ITransformAll
    {
        // Fields
        private Assembly fAssembly;
        private string fAssemblyFile = string.Empty;
        private string fAssemblySource;
        private XPathNavigator fContentXsl = null;
        private XsltGroup fIeFiles = new XsltGroup();
        private bool fIsShowSource;
        private XsltGroup fNavFiles = new XsltGroup();
        private ResourceManager fRm;

        // Methods
        public void Assign(ITransformAll source)
        {
            if (source != null)
            {
                for (HtmlPosition i = HtmlPosition.Head; i <= HtmlPosition.Bottom; i += 1)
                {
                    this[i] = source[i];
                }
                source.IeFiles.AssignTo(this.IeFiles);               
                this.IsShowSource = source.IsShowSource;
            }
        }
        /// <summary>
        /// jinzhao ,without nav
        /// </summary>
        /// <param name="xmlStr"></param>
        /// <param name="position"></param>
        /// <param name="isIe"></param>
        /// <returns></returns>
        protected string GetPartString(string xmlStr, HtmlPosition position)
        {
            if ((position == HtmlPosition.Content) && (this.fContentXsl != null))
            {
                return TransformUtil.Transform(xmlStr, this.fContentXsl);
            }
            return TransformUtil.Transform(xmlStr, this.IeFiles[position]);
            //if (isIe)
            //{
            //    return TransformUtil.Transform(xmlStr, this.IeFiles[position]);
            //}
            //return TransformUtil.Transform(xmlStr, this.NavFiles[position]);
        }

        protected virtual void GetResourceName(TransformPos flags, ref string resourceName)
        {
        }

        public virtual string TransformAll(bool isIe)
        {
            return string.Empty;
        }

        public virtual string TransformAll(bool isIe, TransformPos flags)
        {
            return string.Empty;
        }

        public virtual string TransformContent(bool isIe)
        {
            string content = this[HtmlPosition.Content].GetXmlString();
            if (this.fIsShowSource)
            {
                return content;
            }
            return this.GetPartString(content, HtmlPosition.Content);
        }

        // Properties
        public string AssemblyFile
        {
            get
            {
                return this.fAssemblyFile;
            }
            set
            {
                this.fAssemblyFile = value;
            }
        }

        public string AssemblySource
        {
            get
            {
                return this.fAssemblySource;
            }
            set
            {
                this.fAssemblySource = value;
            }
        }

        public XPathNavigator ContentXsl
        {
            get
            {
                return this.fContentXsl;
            }
            set
            {
                this.fContentXsl = value;
            }
        }

        public XsltGroup IeFiles
        {
            get
            {
                return this.fIeFiles;
            }
        }

        public bool IsShowSource
        {
            get
            {
                return this.fIsShowSource;
            }
            set
            {
                this.fIsShowSource = value;
            }
        }

        public virtual IXmlSource this[HtmlPosition index]
        {
            get
            {
                return null;
            }
            set
            {
            }
        }

        //public XsltGroup NavFiles
        //{
        //    get
        //    {
        //        return this.fNavFiles;
        //    }
        //}

        protected ResourceManager Rm
        {
            get
            {
                if (this.fRm == null)
                {
                    if (this.fAssemblyFile != string.Empty)
                    {
                        this.fAssembly = Assembly.LoadFrom(this.fAssemblyFile);
                    }
                    else
                    {
                        this.fAssembly = Assembly.GetAssembly(base.GetType());
                    }
                    this.fRm = ResourceUtil.GetResourceManager(this.fAssembly, this.fAssemblySource);
                }
                return this.fRm;
            }
        }
    }
}
